﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PKURG.CostSplit.SyncManual.DAL;
using PKURG.CostSplit.SyncService;

namespace PKURG.CostSplit.SyncManual
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadProjects();
        }

        private void LoadProjects()
        {
            CostSplitEntities context = new CostSplitEntities();

            cbl_ProjectList.Items.Clear();
            foreach (var item in context.EProject.ToList())
            {
                cbl_ProjectList.Items.Add(new PInfo(item));
            }
        }

        class PInfo
        {
            public EProject Project { get; set; }
            public PInfo(EProject project)
            {
                this.Project = project;
            }
            public override string ToString()
            {
                return Project.ProjectName;
            }
        }

        private void btnSync_Click(object sender, EventArgs e)
        {
            List<EProject> infos = new List<EProject>();
            foreach (var item in cbl_ProjectList.CheckedItems)
            {
                infos.Add(((PInfo)item).Project);
            }
            new PKURG.CostSplit.SyncService.ERP.SyncData().SyncContractDetailData(cb_h.Checked, cb_uh.Checked, infos);
            MessageBox.Show("同步完成");
        }

        private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelectAll.Checked)
            {
                for (int index = 0; index < cbl_ProjectList.Items.Count; index++)
                {
                    cbl_ProjectList.SetItemChecked(index, true);
                }
            }
            else
            {
                for (int index = 0; index < cbl_ProjectList.Items.Count; index++)
                {
                    cbl_ProjectList.SetItemChecked(index, false);
                }
            }
        }

        private void cbUnSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (cbUnSelect.Checked)
            {
                for (int index = 0; index < cbl_ProjectList.Items.Count; index++)
                {
                    cbl_ProjectList.SetItemChecked(index, !cbl_ProjectList.GetItemChecked(index));
                }
            }
        }

        private void btnErpBase_Click(object sender, EventArgs e)
        {
            ReturnInfo rInfo = new PKURG.CostSplit.SyncService.ERP.SyncData().SyncBaseData();
            LoadProjects();
            MessageBox.Show("同步完成");
        }

        private void btnSyncMY_Click(object sender, EventArgs e)
        {
            ReturnInfo rInfo = new PKURG.CostSplit.SyncService.MING.SyncData().SyncBuildingData();
            MessageBox.Show("同步完成");
        }
    }
}
